<?php
////////
//征集公告
////////
function announce_publish_page(){
	  //我们为这个页面设置标题
	 drupal_set_title('创建公告');
	 
	 $render_array = array(
	   '#markup' => l('查看已发布公告', 'announce/list') . '<br />'. '<br />',
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('announce_publish_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}
/*
//////////////////
//启用所有传人fund_id函数
/////////////////
function announce_publish_perpage($fund_id){
	  //我们为这个页面设置标题
	 drupal_set_title('创建对应征集公告');
	 
	 $render_array = array(
	   '#markup' => l('查看已发布公告', 'announce/list') . '<br />'. '<br />',
	 );
	 $query = "SELECT id, name FROM " .
           "{guiding_fund_info} where id = ?";
	  $queryResult = db_query($query, array($fund_id));
	  foreach($queryResult as $result){
	  	$render_array['#markup'] .= '当前基金：' . '&nbsp;&nbsp;'. $result->name.'<br><br>';
	  }
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('announce_publish_perform',$fund_id);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function announce_publish_perform($form, &$form_state, $perid){	 
	  //提示信息，默认为markup类型
	 $form['bid'] = array('#type' => 'hidden', '#value' => $perid);
	  
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['title'] = array(
	  '#title' => t('标题'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['title'])?$_SESSION['announce_publish_form']['title']:"",
	  //'#description' => t('例如：青岛市市级创业投资引导基金参股设立创业投资基金征集公告'),
	 );
	 
	 $form['abstract'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['abstract'])?$_SESSION['announce_publish_form']['abstract']:"",
	  //'#description' => t('例如：为发展战略性新兴产业，提升自主创新能力，推动青岛市创业投资发展，根据《青岛市创业投资引导基金管理暂行办法》（以下简称《暂行办法》），现面向社会公开征集申请青岛市市级创业投资引导基金参股设立创业投资基金。'),
	 );
	 
	 $form['keywords'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['keywords'])?$_SESSION['announce_publish_form']['keywords']:"",
	  //'#description' => t('例如：2012,青岛，环保，战略性新兴产业'),
	 );
	 
	 $form['body'] = array(
	  '#title' => t('正文'),
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['body'])?$_SESSION['announce_publish_form']['body']:"",
	 );
	 	 
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交'),
	  );
	  return $form;
}

function announce_publish_perform_validate($form, &$form_state){
	  if (drupal_strlen($form_state['values']['title']) > 255) {
	    form_set_error('title', '标题长度不能大于 255 个字符');
	  } else if (drupal_strlen($form_state['values']['keywords']) > 255) {
	    form_set_error('keywords', '关键字长度不能大于 255 个字符');
	  }
  
}

function announce_publish_perform_submit($form, &$form_state){
	  //把表单的值存放在会话中去，由于这里涉及到了两个不同的表单之间传值。
	 $_SESSION['announce_publish_form'] = $form_state['values'];
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'announce/confirm';
}
*/
function announce_confirm_page(){
  //我们为这个页面设置标题
	 drupal_set_title('提交公告预览');
	 
	 //这里首先作了判断，如果会话中没有设置contactus_form，返回contactus
	 if(empty($_SESSION['announce_publish_form'])){
	  drupal_goto('announce/publish');
	 }
	 $render_array = array(
	   '#markup' => '', 
	 );
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('announce_confirm_form');
	 $render_array['#markup'] .= drupal_render($form);
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function announce_list_page() {
  // content variable that will be returned for display
  $page_content = '';
  if(user_access('create announce'))
  	$page_content .= l('发布新公告','announce/publish') . '<br />'. '<br />';
  if(user_access('create announce')){
  $limitnum = 20;
  $query = "SELECT id, title,keywords,abstract, timestamp,endtimestamp FROM " .
           "{announce} WHERE state = ?";
  $queryResult = db_query_range($query,  0, $limitnum, array(0));
  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">暂存公告列表</tr>';
  $count = 0;
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	if($count==0)
	  $page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>关键字</td></tr>';

  	$count += 1;
  	 $page_content .='<tr>';
  	  if(user_access('create announce')){
  	  		$page_content .='<td>'. l($links->title, 'announce/edit/'.$links->id) . '</td>';
	  }
	    
	  else  {
	  	    $page_content .= '<td>'. l($links->title, 'announce/list/'.$links->id). '</td>';		  
	  }
	  
	  $page_content .= '<td>'. $datetime.'</td>';
	  $page_content .= '<td>'. $enddatetime.'</td>';
      $page_content .= '<td>'. $links->abstract.'</td>';
	  $page_content .='</tr>';
  }
  if($count == 0)
  	$page_content .= '<tr><th colspan="4">无暂存公告</tr>';
  $page_content .= '</tbody></table></br>';
  }

  $limitnum = 30;
  $query = "SELECT id, title,keywords,abstract, timestamp,endtimestamp FROM " .
           "{announce} WHERE state = ?";
  $queryResult = db_query_range($query,  0, $limitnum, array(1));
  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">正在征集公告</tr>';
  $count = 0;
  $today = getdate();
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	$enddate = getdate($links->endtimestamp);
	if(($enddate['year']>$today['year'])||(($enddate['year']==$today['year'] )&& ($enddate['mon']>$today['mon']))
	   ||(($enddate['mday']>=$today['mday'])&&($enddate['year']==$today['year'])&&($enddate['mon']==$today['mon']))){
		if($count==0)
	  $page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>关键字</td></tr>';

  	$count += 1;
  	 $page_content .='<tr>';
  	  if(user_access('create announce')){
  	  		$page_content .='<td>'. l($links->title, 'announce/edit/'.$links->id) . '</td>';
	  }
	    
	  else  {
	  	    $page_content .= '<td>'. l($links->title, 'announce/list/'.$links->id). '</td>';		  
	  }
	  
	  $page_content .= '<td>'. $datetime.'</td>';
	  $page_content .= '<td>'. $enddatetime.'</td>';
      $page_content .= '<td>'. $links->abstract.'</td>';
	  $page_content .='</tr>';
	}
  }
  if($count == 0)
  	$page_content .= '<tr><th colspan="4">无正在征集公告</tr>';
  $page_content .= '</tbody></table></br>';
  
  if(user_access('create announce')){
	  $limitnum = 30;
	  $query = "SELECT id, title,keywords,abstract, timestamp,endtimestamp FROM " .
	           "{announce} WHERE state = ?";
	  $queryResult = db_query_range($query,  0, $limitnum, array(1));
	  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">征集结束公告</tr>';
	  $count = 0;
	  foreach ($queryResult as $links ) {
	  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
		$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
		$enddate = getdate($links->endtimestamp);
		if(($enddate['year']<$today['year'])||(($enddate['year']==$today['year'] )&& ($enddate['mon']<$today['mon']))
		   ||(($enddate['mday']<$today['mday'])&&($enddate['year']==$today['year'])&&($enddate['mon']==$today['mon']))){
			if($count==0)
		  $page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>关键字</td></tr>';
	
	  	$count += 1;
	  	 $page_content .='<tr>';
	  	  if(user_access('create announce')){
	  	  		$page_content .='<td>'. l($links->title, 'announce/edit/'.$links->id) . '</td>';
		  }
		    
		  else  {
		  	    $page_content .= '<td>'. l($links->title, 'announce/list/'.$links->id). '</td>';		  
		  }
		  
		  $page_content .= '<td>'. $datetime.'</td>';
		  $page_content .= '<td>'. $enddatetime.'</td>';
	      $page_content .= '<td>'. $links->abstract.'</td>';
		  $page_content .='</tr>';
		}
	  }
	  if($count == 0)
	  	$page_content .= '<tr><th colspan="4">无征集结束公告</tr>';
	  $page_content .= '</tbody></table></br>';
  }
  return $page_content;
}

function announce_list_perpage($perid) {
  // content variable that will be returned for display
  $page_content = '';
 
  if(user_access('create announce')){
  	  $page_content .= l('查看公告列表','announce/managelist') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  	  $page_content .= l('编辑','announce/edit/'.$perid) . '<br />'. '<br />';
  }
  else{
  	$page_content .= l('查看公告列表','announce/list') .  '<br />'. '<br />';
  }
  $query = "SELECT title,abstract,keywords,timestamp,body, endtimestamp,url,file_name FROM " .
           "{announce} where id = ?";
  $queryResult = db_query($query, array($perid));
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d');  
    $page_content .= '<center><h1>'.$links->title.'</h1></center><br /><h4><center>发布时间:  '
    	.$datetime.'</h4></center><br />';
	//if($links->keywords!=NULL)
	  //  $page_content .= '<h4><center>公告简称：'.$links->keywords.'</h4></center><br />';
	//if($links->abstract!=NULL)
    	//$page_content .= '<h4><center>结束时间:  '.$enddatetime.'</h4></center><br />';
	//if($links->abstract!=NULL)
    	//$page_content .= '<h4><center>关键字:  '.$links->abstract.'</h4></center><br />';
    $page_content .= $links->body;
	$page_content = str_replace(array("\n", "\r\n") , "<BR />", $page_content);
	$filename = $links->file_name;
	if($filename != "")
		$page_content .= '<center><h4>附件下载:</h4>'.l($filename, 'http://' .$_SERVER['HTTP_HOST'].'/sites/default/files/'.$links->url).'</center><br />'.'<br />';
  }
  // More coming....
  if ($page_content == '') {
    $page_content = "没有发布该公告.";
  }

  return $page_content;
}

function announce_edit_perpage($perid) {
  // content variable that will be returned for display
  $page_content = '';
  if(user_access('create announce')){
  	  $page_content .= l('查看公告列表','announce/managelist') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  }
  else{
  	  $page_content .= l('查看已发布公告列表','announce/list') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
  }
  $page_content .= l('浏览','announce/list/'.$perid) . '<br />'. '<br />';
 
  drupal_set_title('管理查看公告');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('announce_edit_form',$perid);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function announce_edit_form($form, &$form_state, $perid){
  $query = "SELECT title,abstract,keywords,timestamp,body,endtimestamp,file_name, url, file_managed_id FROM " .
           "{announce} where id = ?";
  $queryResult = db_query($query, array($perid));
 
  $title = '';
  $abstract = '';
  $keywords = '';
  $body = '';
  $enddatetime = '';
  foreach ($queryResult as $links ) {
  	  $title = $links->title;
	  $abstract = $links->abstract;
	  $keywords = $links->keywords;
	  $body = $links->body;
	  $enddatetime = getdate($links->endtimestamp);
	  $file_name =  $links->file_name;
	  $url =  $links->url;
	  $file_managed_id = $links->file_managed_id;
  }
  $form['bid'] = array('#type' => 'hidden', '#value' => $perid);
  $form['file_managed_id'] = array('#type' => 'hidden', '#value' => $file_managed_id);  
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['title'] = array(
	  '#title' => t('标题'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['announce_edit_form']['title'])?$_SESSION['announce_edit_form']['title']:$title,
	  //'#description' => t('例如：青岛市市级创业投资引导基金参股设立创业投资基金征集公告'),
	 );
	 $form['keywords'] = array(
	  '#title' => t('公告简称'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_edit_form']['keywords'])?$_SESSION['announce_edit_form']['keywords']:$keywords,
	  //'#description' => t('例如：2012,青岛，环保，战略性新兴产业'),
	 );
	 
	 $form['abstract'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_edit_form']['abstract'])?$_SESSION['announce_edit_form']['abstract']:$abstract,
	  //'#description' => t('例如：为发展战略性新兴产业，提升自主创新能力，推动青岛市创业投资发展，根据《青岛市创业投资引导基金管理暂行办法》（以下简称《暂行办法》），现面向社会公开征集申请青岛市市级创业投资引导基金参股设立创业投资基金。'),
	 );
	
	 $form['endtimestamp'] = array(
	   '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	   '#title' => t('结束时间'),
	   '#default_value' => isset($_SESSION['announce_edit_form']['endtimestamp'])?$_SESSION['announce_edit_form']['endtimestamp']:
		     	array('year' =>  $enddatetime['year'], 'month' =>  $enddatetime['mon'], 'day' => $enddatetime['mday']), 
	 );
     
	 $form['body'] = array(
	  '#title' => t('正文'),
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['announce_edit_form']['body'])?$_SESSION['announce_edit_form']['body']:$body,
	 );
  
     $form['file_name'] = array(
	  '#title' => t('文件名'),
	  '#type' => 'textfield',
	  '#required' => FAlSE,
	  '#default_value' =>isset($_SESSION['announce_edit_form']['file_name'])?$_SESSION['announce_edit_form']['file_name']:$file_name,
	 );
	 
	 $form['upload'] = array(
	  '#type' => 'managed_file',
	  '#title' => '重新提交附件',
	  '#required' => FAlSE,
	  '#upload_location' => 'public://my_uploads',
	  '#upload_validators' => array(
	    'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(50*1024*1024)),
	);
	 
	 
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('暂存'),
    '#submit' => array('announce_edit_form_save'),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('发布'),
  );
   
   $form['deleate'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
    '#submit' => array('announce_edit_form_deleate'),
  );
  return $form;
}

function announce_edit_form_save($form, &$form_state){
	 $values = NULL;
	  $values = $form_state['values'];
      $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	   $datetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
		
	$fid = $form_state['values']['upload']; 
	$filename = '';
	$file = file_load($fid);  

	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	$filemanagedid = $values['file_managed_id'];
	$uploadid = $values['bid'];
	
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
		$fid = $file->fid;   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
								
			if($filemanagedid>0){
				$file = file_load($filemanagedid);
				  if($file!=NULL){
					 file_delete($file);
				  }
				db_delete('file_managed')
				  ->condition('fid',$filemanagedid)
				  ->execute();
			}	
			db_update('announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 'abstract' => $values['abstract'],
	   			 'keywords' =>  $values['keywords'],
	   			 'timestamp' => $_SERVER['REQUEST_TIME'],
	   			 'body' => $page_content,
	   			 'endtimestamp' =>$datetime,
	   			 'state' => 0,
	   			 'file_name' => $filename,
			   	 'url' => $saveurl,
			   	 'file_managed_id' => $fid,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
			
			$form_state['redirect'] = 'announce/list/'.$values['bid'];
		}			
		else {
			form_set_error('upload',$extension_errors[0]); 
		}
	}
	else{
		db_update('announce')
  		->fields(array(
             'title' => $values['title'],
   			 'abstract' => $values['abstract'],
   			 'keywords' =>  $values['keywords'],
   			 'timestamp' => $_SERVER['REQUEST_TIME'],
   			 'body' => $page_content,
   			 'endtimestamp' =>$datetime,
   			 'file_name' => $filename,
   			 'state' => 0,
  			))
  		->condition('id', $values['bid'])
  		->execute();
		$form_state['redirect'] = 'announce/list/'.$values['bid'];
	}
}

function announce_edit_form_submit($form, &$form_state){
	 $values = NULL;
	  $values = $form_state['values'];
      $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	   $datetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
		
	$fid = $form_state['values']['upload']; 
	$filename = '';
	$file = file_load($fid);  

	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	$filemanagedid = $values['file_managed_id'];
	$uploadid = $values['bid'];
	
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
		$fid = $file->fid;   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
								
			if($filemanagedid>0){
				$file = file_load($filemanagedid);
				  if($file!=NULL){
					 file_delete($file);
				  }
				db_delete('file_managed')
				  ->condition('fid',$filemanagedid)
				  ->execute();
			}	
			db_update('announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 'abstract' => $values['abstract'],
	   			 'keywords' =>  $values['keywords'],
	   			 'timestamp' => $_SERVER['REQUEST_TIME'],
	   			 'body' => $page_content,
	   			 'endtimestamp' =>$datetime,
	   			 'state' => 1,
	   			 'file_name' => $filename,
			   	 'url' => $saveurl,
			   	 'file_managed_id' => $fid,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
			
			$form_state['redirect'] = 'announce/list/'.$values['bid'];
		}			
		else {
			form_set_error('upload',$extension_errors[0]); 
		}
	}
	else{
		db_update('announce')
  		->fields(array(
             'title' => $values['title'],
   			 'abstract' => $values['abstract'],
   			 'keywords' =>  $values['keywords'],
   			 'timestamp' => $_SERVER['REQUEST_TIME'],
   			 'body' => $page_content,
   			 'endtimestamp' =>$datetime,
   			 'state' => 1,
   			 'file_name' => $filename,
  			))
  		->condition('id', $values['bid'])
  		->execute();
		$form_state['redirect'] = 'announce/list/'.$values['bid'];
	}
	
	 
}

function announce_edit_form_deleate($form, &$form_state){
	 $values = NULL;
	 $_SESSION['announce_edit_form'] = $form_state['values'];
	 $values = $_SESSION['announce_edit_form'] ;
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'announce/deleate/'.$values['bid'];
}

function announce_deleate_page($perid) {
  // content variable that will be returned for display
  $page_content = '删除后该公告内容将不可恢复！';
  
  drupal_set_title('删除公告');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('announce_deleate_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function announce_deleate_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('announce_deleate_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
  );
  
  return $form;
}

function announce_deleate_form_back($form, &$form_state){
	if(empty($_SESSION['announce_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'announce/list';
	  }else{
		  $values = $_SESSION['announce_edit_form'];
		  unset($_SESSION['announce_edit_form']);
		  $form_state['redirect'] = 'announce/edit/'.$values['bid'];
	  }
}

function announce_deleate_form_submit($form, &$form_state){
  //简单的重定向到contactus页面
 if(empty($_SESSION['announce_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'announce/list';
	  }else{
		  $values = $_SESSION['announce_edit_form'];
		  unset($_SESSION['announce_edit_form']);
		  $file = file_load($values['file_managed_id']);
				  if($file!=NULL){
					 file_delete($file);
				  }
		  db_delete('file_managed')
				  ->condition('fid',$values['file_managed_id'])
				  ->execute();
		  db_delete('announce')
			->condition('id' ,$values['bid'])
			->execute();
		  $form_state['redirect'] = 'announce/list';;
	  }

}

function announce_confirm_form(){
 //表单元素“姓名”
 $body = $_SESSION['announce_publish_form']['body'];
 $page_content = '';
 foreach($body as $content){
 	if($content != 'plain_text')
 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
 }
 $form['title'] = array(
  '#title' => t('标题'),
  //表单元素的类型,这里为item
  '#type' => 'item',
  '#markup' => isset($_SESSION['announce_publish_form']['title'])?$_SESSION['announce_publish_form']['title']:"",
 );
  //表单元素“电子邮件”
 $form['keywords'] = array(
  '#title' => t('公告简称'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['announce_publish_form']['keywords'])?$_SESSION['announce_publish_form']['keywords']:"",
 );
 
  //表单元素“单位名称”
 $form['abstract'] = array(
  '#title' => t('关键字'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['announce_publish_form']['abstract'])?$_SESSION['announce_publish_form']['abstract']:"",
 );
 
 $enddatetime = $_SESSION['announce_publish_form']['endtimestamp'];
 $form['endtimestamp'] = array(
   '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
   '#title' => t('结束时间'),
   '#default_value' => isset($_SESSION['announce_publish_form']['endtimestamp'])?$_SESSION['announce_publish_form']['endtimestamp']:
	     	array('year' =>  $enddatetime['year'], 'month' =>  $enddatetime['mon'], 'day' => $enddatetime['mday']), 
 );
 //表单元素“电话号码”
 $form['body'] = array(
  '#title' => t('正文'),
  '#type' => 'item',
  '#markup' => $page_content,
 );
 

  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('返回'),
    '#submit' => array('announce_confirm_form_back'),
  );

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('暂存'),
    '#submit' => array('announce_confirm_form_save'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('发布'),
  );
  
  return $form;
}

function announce_publish_form(){	 
	  //提示信息，默认为markup类型。
	 //$form['bid'] = array('#type' => 'hidden', '#value' => 0);
		 $form['tips'] = array(
		  '#prefix' =>'<div id="tips">',
		  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
		  '#suffix' =>'</div>',
		 );
		 
	 //表单元素“姓名”
	 $form['title'] = array(
	   //表单元素的#title属性，对应于实际输出中的label
	  '#title' => t('标题'),
	  //表单元素的类型，这里为textfield
	  '#type' => 'textfield',
	  //这个表单元素是必填的
	  '#required' => TRUE,
	  //表单元素的默认值，这里使用了三位运算符和isset进行判定
	  '#default_value' =>isset($_SESSION['announce_publish_form']['title'])?$_SESSION['announce_publish_form']['title']:"",
	  //表单元素的描述，
	 // '#description' => t('例如：青岛市市级创业投资引导基金参股设立创业投资基金征集公告'),
	 );
	 
	   //表单元素“电子邮件”
	 $form['keywords'] = array(
	  '#title' => t('公告简称'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['keywords'])?$_SESSION['announce_publish_form']['keywords']:"",
	 // '#description' => t('例如：2012,青岛，环保，战略性新兴产业'),
	 );
	 
	  //表单元素“单位名称”
	 $form['abstract'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['abstract'])?$_SESSION['announce_publish_form']['abstract']:"",
	 // '#description' => t('例如：为发展战略性新兴产业，提升自主创新能力，推动青岛市创业投资发展，根据《青岛市创业投资引导基金管理暂行办法》（以下简称《暂行办法》），现面向社会公开征集申请青岛市市级创业投资引导基金参股设立创业投资基金。'),
	 );
	 
	 $today = getdate();
     $todaytimestamp =  mktime(0, 0, 0,$today['mon'], $today['mday'], $today['year']);
	  
 	 $enddatetime =  getdate($todaytimestamp+30*24*3600);
      $form['endtimestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('结束时间'),
	     '#default_value' => array('year' =>  $enddatetime['year'], 'month' =>  $enddatetime['mon'], 'day' => $enddatetime['mday']), 
	  );
	  
	  //表单元素“邮件正文”
	 $form['body'] = array(
	  '#title' => t('正文'),
	  //表单元素的类型，这里为textarea
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['announce_publish_form']['body'])?$_SESSION['announce_publish_form']['body']:"",
	 );
	 
	  $form['upload'] = array(
	   '#type' => 'managed_file',
	   '#title' => '附件上传',
	   //'#required' => TRUE,
	   '#upload_location' => 'public://my_uploads',
	   '#upload_validators' => array(
	     'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		 'file_validate_size' => array(50*1024*1024)),
	 );
	
	 $form['file_name'] = array(
	  '#title' => t('显示文件名：'),
	  '#type' => 'textfield',
	  //'#required' => TRUE,
	  '#default_value' =>"附件",
	 );	
	 
	 //表单元素“确认”提交按钮
	  //$form['submit'] = array(
	    //'#type' => 'submit',
	    //'#value' => t('确认'),
	  //);
	  
	   $form['back'] = array(
	    '#type' => 'submit',
	    '#value' => t('清空'),
	    '#submit' => array('announce_publish_form_back'),
	  );
	
	  $form['save'] = array(
	    '#type' => 'submit',
	    '#value' => t('暂存'),
	    '#submit' => array('announce_publish_form_save'),
	  );
	
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('发布'),
	  );
	  return $form;
}

function announce_publish_form_validate($form, &$form_state){
	  /**
	   * 用户提交的所有数据在 $form_state['values'] 之中，相当于 $_POST
	   * 根据需求，验证昵称是否少于 2 个字符，中英文均算一个字符
	   * 可使用封装好的计算字符串长度函数：drupal_strlen
	   */
	  if (drupal_strlen($form_state['values']['title']) > 255) {
	    form_set_error('title', '标题长度不能大于 255 个字符');
	  } else if (drupal_strlen($form_state['values']['keywords']) > 255) {
	    form_set_error('keywords', '公告简称长度不能大于 255 个字符');
	  }
  
}

function announce_publish_form_submit($form, &$form_state){
	  //把表单的值存放在会话中去，由于这里涉及到了两个不同的表单之间传值。
	 $_SESSION['announce_publish_form'] = $form_state['values'];
	 //表单重定向到确认页面
	// $form_state['redirect'] = 'announce/confirm';
	 $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['announce_publish_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'announce/publish';
	  }else{
		  $values = $_SESSION['announce_publish_form'];
		  unset($_SESSION['announce_publish_form']);
	  }
	  
	 
	  
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);			
			$body = $values['body'];
			
			 $page_content = '';
			 foreach($body as $content){
			 	if($content != 'plain_text')
			 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
			 }
			  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
			
			  $last_id = db_insert('announce')
		  		->fields(array(
		   			 'title' => $values['title'],
		   			 'abstract' => $values['abstract'],
		   			 'keywords' =>  $values['keywords'],
		   			 'timestamp' => $_SERVER['REQUEST_TIME'],
		   			 'endtimestamp' =>  $enddatetime,
		   			 'body' => $page_content,
		   			 'state' => 1,
		   			 'url' => $saveurl,
		   			 'file_name' => $filename,	 
			   		 'file_managed_id' => $fid,
		  			))->execute();
			  if ($last_id!=NULL) {
			     /*if($values['bid']>0)
			  		{
			  			db_update('guiding_fund_info')
				  		->fields(array(
				   			 'announce_id' => $last_id,
				  			))
				  		->condition('id', $values['bid'])
				  		->execute();
						$form_state['redirect'] = 'guidingfund/list';
			  		}
				 else*/
					 $form_state['redirect'] = 'announce/managelist/';
				 	    
			  } else {
			    // 数据写入失败了
			    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');  
			  }
				} 
				else {
				form_set_error('upload',$extension_errors[0]); 
				}
	}
else{
		
		 $body = $values['body'];
		 $page_content = '';
		 foreach($body as $content){
		 	if($content != 'plain_text')
		 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
		 }
		 $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
		  
		  $last_id = db_insert('announce')
	  		->fields(array(
	   			 'title' => $values['title'],
	   			 'abstract' => $values['abstract'],
	   			 'keywords' =>  $values['keywords'],
	   			 'endtimestamp' =>  $enddatetime,
	   			 'timestamp' => $_SERVER['REQUEST_TIME'],
	   			 'body' => $page_content,
	   			 'endtimestamp' =>  $enddatetime,
	   			 'state' => 1,
	  			))->execute();
		  if ($last_id!=NULL) {
		     /*	
		     if($values['bid']>0)
		  		{
		  			db_update('guiding_fund_info')
			  		->fields(array(
			   			 'announce_id' => $last_id,
			  			))
			  		->condition('id', $values['bid'])
			  		->execute();
					$form_state['redirect'] = 'guidingfund/list';
		  		}
			 else*/
				 $form_state['redirect'] = 'announce/managelist';
			 	    
		  } else {
		    // 数据写入失败了
		    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
		  }	
		
	}
}

function announce_publish_form_back($form, &$form_state){
  //简单的重定向到contactus页面
 $form_state['redirect'] = 'announce/publish';
}
/*
function announce_publish_form_submit($form, &$form_state){
	   $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['announce_publish_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'announce/publish';
	  }else{
		  $values = $_SESSION['announce_publish_form'];
		  unset($_SESSION['announce_publish_form']);
	  }
	  
	 $body = $values['body'];
	 $page_content = '';
	 foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	 }
	  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
	  
	  $last_id = db_insert('announce')
  		->fields(array(
   			 'title' => $values['title'],
   			 'abstract' => $values['abstract'],
   			 'keywords' =>  $values['keywords'],
   			 'timestamp' => $_SERVER['REQUEST_TIME'],
   			 'endtimestamp' =>  $enddatetime,
   			 'body' => $page_content,
   			 'state' => 1,
  			))->execute();
	  if ($last_id!=NULL) {
	    
		 $form_state['redirect'] = 'announce/managelist';
		 	    
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }
}
*/
function announce_publish_form_save($form, &$form_state){
	   $_SESSION['announce_publish_form'] = $form_state['values'];
	   $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['announce_publish_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'announce/publish';
	  }else{
		  $values = $_SESSION['announce_publish_form'];
		  unset($_SESSION['announce_publish_form']);
	  }
	  
	  $fid = $form_state['values']['upload']; 

	$file = file_load($fid);  
	
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
		$fid = $file->fid;   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);			
			
		  $body = $values['body'];
		 $page_content = '';
		 foreach($body as $content){
		 	if($content != 'plain_text')
		 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
		 }
		 $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
		  
		  $last_id = db_insert('announce')
	  		->fields(array(
	   			 'title' => $values['title'],
	   			 'abstract' => $values['abstract'],
	   			 'keywords' =>  $values['keywords'],
	   			 'endtimestamp' =>  $enddatetime,
	   			 'timestamp' => $_SERVER['REQUEST_TIME'],
	   			 'body' => $page_content,
	   			 'endtimestamp' =>  $enddatetime,
	   			 'state' => 0,
	   			 'url' => $saveurl,
		   		 'file_name' => $filename,	 
			   	 'file_managed_id' => $fid,
	  			))->execute();
		  if ($last_id!=NULL) {
		     /*if($values['bid']>0)
		  		{
		  			db_update('guiding_fund_info')
			  		->fields(array(
			   			 'announce_id' => $last_id,
			  			))
			  		->condition('id', $values['bid'])
			  		->execute();
					$form_state['redirect'] = 'announce/managelist';
		  		}
			 else*/
				 $form_state['redirect'] = 'announce/managelist';
			 	    
		  } else {
		    // 数据写入失败了
		    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
		  }	
	}
	else {
			form_set_error('upload',$extension_errors[0]); 
		}
	}
	else{
		
		 $body = $values['body'];
		 $page_content = '';
		 foreach($body as $content){
		 	if($content != 'plain_text')
		 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
		 }
		 $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
		  
		  $last_id = db_insert('announce')
	  		->fields(array(
	   			 'title' => $values['title'],
	   			 'abstract' => $values['abstract'],
	   			 'keywords' =>  $values['keywords'],
	   			 'endtimestamp' =>  $enddatetime,
	   			 'timestamp' => $_SERVER['REQUEST_TIME'],
	   			 'body' => $page_content,
	   			 'endtimestamp' =>  $enddatetime,
	   			 'state' => 0,
	  			))->execute();
		  if ($last_id!=NULL) {
		     /*if($values['bid']>0)
		  		{
		  			db_update('guiding_fund_info')
			  		->fields(array(
			   			 'announce_id' => $last_id,
			  			))
			  		->condition('id', $values['bid'])
			  		->execute();
					$form_state['redirect'] = 'announce/managelist';
		  		}
			 else*/
				 $form_state['redirect'] = 'announce/managelist';
			 	    
		  } else {
		    // 数据写入失败了
		    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
		  }	
		
	}
}


////////
//过会企业公示
////////

function enterprise_announce_publish_page(){
	  //我们为这个页面设置标题
	 drupal_set_title('创建过会企业公示');
	 
	 $render_array = array(
	   '#markup' => l('查看已发布公示列表', 'enterprise_announce/list') . '<br />'. '<br />',
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('enterprise_announce_publish_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}
/*
function enterprise_announce_publish_perpage($fund_id){
	  //我们为这个页面设置标题
	 drupal_set_title('创建过会企业公示');
	 
	 $render_array = array(
	   '#markup' => l('查看已发布过会企业公示', 'enterprise_announce/list') . '<br />'. '<br />',
	 );
	 $query = "SELECT id, name FROM " .
           "{guiding_fund_info} where id = ?";
	  $queryResult = db_query($query, array($fund_id));
	  foreach($queryResult as $result){
	  	$render_array['#markup'] .= '当前基金：' . '&nbsp;&nbsp;'. $result->name.'<br><br>';
	  }
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('enterprise_announce_publish_perform',$fund_id);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}
 function enterprise_announce_confirm_page(){
  //我们为这个页面设置标题
	 drupal_set_title('提交过会企业公示预览');
	 
	 //这里首先作了判断，如果会话中没有设置contactus_form，返回contactus
	 if(empty($_SESSION['enterprise_announce_publish_form'])){
	  drupal_goto('enterprise_announce/publish');
	 }
	 $render_array = array(
	   '#markup' => '', 
	 );
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('enterprise_announce_confirm_form');
	 $render_array['#markup'] .= drupal_render($form);
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}
 
*/

function enterprise_historylist_page(){
	
}

function enterprise_announce_list_page() {
  // content variable that will be returned for display
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
 
  $page_content = '';
  if(user_access('create announce'))
  	$page_content .= l('发布新公告','enterprise_announce/publish') . '<br />'. '<br />';
  if(user_access('create announce')){
  $limitnum = 20;
  $query = "SELECT id, title, timestamp,endtimestamp,funds_list FROM " .
           "{enterprise_announce} WHERE state = ?";
  $queryResult = db_query_range($query,  0, $limitnum, array(0));
  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">暂存公告列表</tr>';
  $count = 0;
  $funds_array =  array();
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	if($count==0)
	  $page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>公告企业</td></tr>';
      $funds_array = explode(',',$links->funds_list);
	  $funds_name = '';
      $count += 1;
  	  $page_content .='<tr>';

	  $page_content .= '<td>'. l($links->title, 'enterprise_announce/list/'.$links->id). '</td>';		  
	  
	  
	  $page_content .= '<td>'. $datetime.'</td>';
	  $page_content .= '<td>'. $enddatetime.'</td>';
	  foreach ($funds_array as $fund_id) {
	  	if($fund_id>0){
	  		$fund_info = get_info_by_id($fund_id);
		    $funds_name .= l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']).',';
	    }
	  }
      $page_content .= '<td>'. $funds_name.'</td>';
	  $page_content .='</tr>';
  }
  if($count == 0)
  	$page_content .= '<tr><th colspan="4">无暂存公告</tr>';
  $page_content .= '</tbody></table></br>';
  }

  $limitnum = 30;
  $query = "SELECT id, title, timestamp,endtimestamp,funds_list FROM " .
           "{enterprise_announce} WHERE state = ?";
  $queryResult = db_query_range($query,  0, $limitnum, array(1));
  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">公示期公告列表</tr>';
  $count = 0;
  $today = getdate();
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	$enddate = getdate($links->endtimestamp);
	if(($enddate['year']>$today['year'])||(($enddate['year']==$today['year'] )&& ($enddate['mon']>$today['mon']))
	   ||(($enddate['mday']>=$today['mday'])&&($enddate['year']==$today['year'])&&($enddate['mon']==$today['mon']))){
			
	  if($count==0)
	  		$page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>公告企业</td></tr>';
      $funds_array = explode(',',$links->funds_list);
	  $funds_name = '';
  	  $count += 1;
  	  $page_content .='<tr>';
	  $page_content .= '<td>'. l($links->title, 'enterprise_announce/list/'.$links->id). '</td>';		   
	  $page_content .= '<td>'. $datetime.'</td>';
	  $page_content .= '<td>'. $enddatetime.'</td>';
      foreach ($funds_array as $fund_id) {
      	if($fund_id>0){
	  	  $fund_info = get_info_by_id($fund_id);
		  $funds_name .= l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']).',';
		}
	  }
      $page_content .= '<td>'. $funds_name.'</td>';
	  $page_content .='</tr>';
	}
  }
  if($count == 0)
  	$page_content .= '<tr><th colspan="4">无公示期公告</tr>';
  $page_content .= '</tbody></table></br>';
  
  if(user_access('create announce')){
	  $limitnum = 30;
	  $query = "SELECT id, title, timestamp,endtimestamp,funds_list FROM " .
	           "{enterprise_announce} WHERE state = ?";
	  $queryResult = db_query_range($query,  0, $limitnum, array(1));
	  $page_content .= '<table style="width:900px"><tbody><tr><th colspan="4">公示期结束公告</tr>';
	  $count = 0;
	  foreach ($queryResult as $links ) {
	  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
		$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
		$enddate = getdate($links->endtimestamp);
		if(($enddate['year']<$today['year'])||(($enddate['year']==$today['year'] )&& ($enddate['mon']<$today['mon']))
		   ||(($enddate['mday']<$today['mday'])&&($enddate['year']==$today['year'])&&($enddate['mon']==$today['mon']))){
		  	
		  if($count==0)
		      $page_content .='<tr><td width=\'25%\'>公告标题</td><td width=\'13%\'>发布时间</td><td width=\'13%\'>结束时间</td><td width=\'49%\'>关键字</td></tr>';
	      
	      $funds_array = explode(',',$links->funds_list);
	      $funds_name = '';
	  	  $count += 1;
	  	  $page_content .='<tr>';

		  $page_content .= '<td>'. l($links->title, 'enterprise_announce/list/'.$links->id). '</td>';		  
		  
		  $page_content .= '<td>'. $datetime.'</td>';
		  $page_content .= '<td>'. $enddatetime.'</td>';
	      foreach ($funds_array as $fund_id) {
	      	if($fund_id>0){
	  	  		$fund_info = get_info_by_id($fund_id);
		   		$funds_name .= l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']).',';
			}
	  	  }
      $page_content .= '<td>'. $funds_name.'</td>';
		  $page_content .='</tr>';
		}
	  }
	  if($count == 0)
	  	$page_content .= '<tr><th colspan="4">无公示期结束公告</tr>';
	  $page_content .= '</tbody></table></br>';
  }
  return $page_content;
}

function enterprise_announce_list_perpage($perid) {
  // content variable that will be returned for display
  $page_content = '';
  if(user_access('create announce')){
  	  $page_content .= l('查看已发布公示列表','enterprise_announce/list') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	  $query = "SELECT state FROM " .
           "{enterprise_announce} where id = ?";
 	  $queryResult = db_query($query, array($perid));
	  foreach ($queryResult as $links ) {
  	     // if($links->state == 0)
  	  	  		$page_content .= l('编辑','enterprise_announce/edit/'.$perid) . '<br />'. '<br />';
	  }
  }
  else{
  	  $page_content .= l('查看过会企业公示列表','enterprise_announce/list') . '<br />'. '<br />';
  }

  $query = "SELECT title,timestamp, endtimestamp, body FROM " .
           "{enterprise_announce} where id = ?";
  $queryResult = db_query($query, array($perid));
  foreach ($queryResult as $links ) {
  	$datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
    $page_content .= '<center><h1>'.$links->title.'</h1></center><br /><h4><center>'
    	.$datetime.'</h4></center><br />';
	/*
	if($links->keywords!=NULL)
	    $page_content .= '<h4><center>关键词：'.$links->keywords.'</h4></center><br />';
	if($links->abstract!=NULL)
    	$page_content .= '<h4><center>关键字：'.$links->abstract.'</h4></center><br />';
    */
	$page_content .= $links->body;
	$page_content = str_replace(array("\n", "\r\n") , "<BR />", $page_content);
  }
  // More coming....
  if ($page_content == '') {
    $page_content = "没有发布该过会企业公示.";
  }

  return $page_content;
}

function enterprise_announce_edit_perpage($perid) {
  // content variable that will be returned for display
  /*$query = "SELECT state FROM " .
           "{enterprise_announce} where id = ?";
  $queryResult = db_query($query, array($perid));
  foreach ($queryResult as $links ) {
  	if($links->state == 1)
	  drupal_goto('enterprise_announce/list/'.$perid);
  }*/
  
  $page_content = '';
  $page_content .= l('查看已发布公示列表','enterprise_announce/list') . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
  $page_content .= l('浏览','enterprise_announce/list/'.$perid) . '<br />'. '<br />';
  drupal_set_title('管理查看过会企业公示');
	
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('enterprise_announce_edit_form',$perid);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function enterprise_announce_edit_form($form, &$form_state, $perid){
  $query = "SELECT title,timestamp, endtimestamp,body,state FROM " .
           "{enterprise_announce} where id = ?";
  $queryResult = db_query($query, array($perid));
  $title = '';
  //$abstract = '';
  //$keywords = '';
  $datetime = '';
  $enddatetime = '';
  $body = '';
  $state = '';
  foreach ($queryResult as $links ) {
  	  $title = $links->title;
	  $datetime = getdate($links->timestamp);
	  $enddatetime = getdate($links->endtimestamp);
	  $body = $links->body;
	  $state = $links->state;
  }

  $form['bid'] = array('#type' => 'hidden', '#value' => $perid);
  $form['state'] = array('#type' => 'hidden', '#value' => $state);
  $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['title'] = array(
	  '#title' => t('标题'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_edit_form']['title'])?$_SESSION['enterprise_announce_edit_form']['title']:$title,
	 );
	 /*
	 $form['abstract'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['abstract'])?$_SESSION['enterprise_announce_publish_form']['abstract']:$abstract,
	  );
	 
	 $form['keywords'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['keywords'])?$_SESSION['enterprise_announce_publish_form']['keywords']:$keywords,
	 );
	 */
 	 
 	 $form['timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('开始时间'),
	     '#default_value' => isset($_SESSION['enterprise_announce_edit_form']['timestamp'])?$_SESSION['enterprise_announce_edit_form']['timestamp']:
		     	array('year' =>  $datetime['year'], 'month' =>  $datetime['mon'], 'day' => $datetime['mday']), 
	 );
     
     $form['endtimestamp'] = array(
	   '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	   '#title' => t('结束时间'),
	   '#default_value' => isset($_SESSION['enterprise_announce_edit_form']['endtimestamp'])?$_SESSION['enterprise_announce_edit_form']['endtimestamp']:
		     	array('year' =>  $enddatetime['year'], 'month' =>  $enddatetime['mon'], 'day' => $enddatetime['mday']), 
	 );
	 
	  if($state == 0){
	 	module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  $fund_list = get_part_by_state(9);
	  $header = array(
	    'nishejijinmingcheng' => t('拟申请基金名称'),
	    'jijinguimo' => t('基金规模'),
	    'zhuyaotouzilingyu' => t('主投领域'),
	    'guanlijigoumingcheng' => t('机构名称'),
	    'lianxiren' => t('联系人'),
	  );  
	
		 
	  $options = array();
	  foreach ($fund_list as $fund) {
	    $options[$fund['id']] =array(
	      'nishejijinmingcheng' => $fund['nishejijinmingcheng'],
	      'jijinguimo' => $fund['jijinguimo'],  
	      'zhuyaotouzilingyu' => $fund['zhuyaotouzilingyu'],
	      'guanlijigoumingcheng' => $fund['guanlijigoumingcheng'],
	      'lianxiren' => $fund['lianxiren'],  
	    );
	  }
	
	  $form['table'] = array(
	    '#type' => 'tableselect',
	    '#header' => $header,
	    
	    '#options' => $options,
	    '#empty' => t('没有基金记录'),
	    
	  );
	 } 
	 $form['body'] = array(
	  '#title' => t('正文'),
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_edit_form']['body'])?$_SESSION['enterprise_announce_edit_form']['body']:$body,
	 );
	
	 if($state == 0){ 
	  $form['save'] = array(
        '#type' => 'submit',
        '#value' => t('暂存'),
        '#submit' => array('enterprise_announce_edit_form_save'),
      ); 
     
	 }
	 	 
     $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('发布'),
     );
     $form['deleate'] = array(
        '#type' => 'submit',
        '#value' => t('删除'),
        '#submit' => array('enterprise_announce_edit_form_deleate'),
     );
  
  return $form;
}

function enterprise_announce_edit_form_save($form, &$form_state){
	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
	  $values = NULL;
	  $values = $form_state['values'];
	  $state = $values['state'];
      $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $datetime =  mktime(0, 0, 0,$values['timestamp']['month'], $values['timestamp']['day'], $values['timestamp']['year']);
	  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
	  
	  $funds_list = array();
	  foreach ($values['table'] as $rows) {
         $funds_list[] =$rows;
      }
	  $funds_array = get_part_by_state(9);
	  $funds_list = array();
	  if($state == 0){
	  	  $funds_list = array();
		  foreach ($values['table'] as $rows) {
	         $funds_list[] =$rows;
	      }
	      $funds_list = '';
		  $funds_array = get_part_by_state(9);
	  }
	  if($funds_list!=NULL){
	  	  foreach ($funds_array as $fund) {
	         if(in_array($fund['id'],$funds_list, TRUE)){       	
				$funds_list .= $fund['id'].',';
	         }
	      }
		  db_update('enterprise_announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 //'abstract' => $values['abstract'],
	   			 //'keywords' =>  $values['keywords'],
	   			 'timestamp' => $datetime,
	   			 'endtimestamp' => $enddatetime,
	   			 'body' =>$page_content,
	   			 'fund_list' => $funds_list,
	   			 'state' => 0,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
	  }
	  else{
		  db_update('enterprise_announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 //'abstract' => $values['abstract'],
	   			 //'keywords' =>  $values['keywords'],
	   			 'timestamp' => $datetime,
	   			 'endtimestamp' => $enddatetime,
	   			 'body' =>$page_content,
	   			 'state' => 0,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
	  	
	  }
	  $form_state['redirect'] = 'enterprise_announce/list/'.$values['bid'];
}

function enterprise_announce_edit_form_submit($form, &$form_state){
	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
	  	
	  $values = NULL;
	  $values = $form_state['values'];
      $body = $values['body'];
	  $state = $values['state'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $datetime =  mktime(0, 0, 0,$values['timestamp']['month'], $values['timestamp']['day'], $values['timestamp']['year']);
	  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
	  $funds_list = array();
	  if($state == 0){
	  	  $funds_list = array();
		  foreach ($values['table'] as $rows) {
	         $funds_list[] =$rows;
	      }
	      $funds_list = '';
		  $funds_array = get_part_by_state(9);
	  }
	  
	  if($funds_list!=NULL){
	  	  foreach ($funds_array as $fund) {
	         if(in_array($fund['id'],$funds_list, TRUE)){       	
				set_state_by_id(11,$fund['id']);
				$funds_list .= $fund['id'].',';
	         }
			 else{
			 	set_state_by_id(10,$fund['id']);
			 }
	      }
		  db_update('enterprise_announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 //'abstract' => $values['abstract'],
	   			 //'keywords' =>  $values['keywords'],
	   			 'timestamp' => $datetime,
	   			 'endtimestamp' => $enddatetime,
	   			 'body' =>$page_content,
	   			 'fund_list' => $funds_list,
	   			 'state' => 1,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
	  }
	  else{
		  db_update('enterprise_announce')
	  		->fields(array(
	             'title' => $values['title'],
	   			 //'abstract' => $values['abstract'],
	   			 //'keywords' =>  $values['keywords'],
	   			 'timestamp' => $datetime,
	   			 'endtimestamp' => $enddatetime,
	   			 'body' =>$page_content,
	   			 'state' => 1,
	  			))
	  		->condition('id', $values['bid'])
	  		->execute();
	  	
	  }
	  
	  $form_state['redirect'] = 'enterprise_announce/list/'.$values['bid'];
}

function enterprise_announce_edit_form_deleate($form, &$form_state){
	 $values = NULL;
	 $_SESSION['enterprise_announce_edit_form'] = $form_state['values'];
	 $values = $_SESSION['enterprise_announce_edit_form'] ;
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'enterprise_announce/deleate/'.$values['bid'];
}

function enterprise_announce_deleate_page($perid) {
  // content variable that will be returned for display
  $page_content = '删除后该过会企业公示内容将不可恢复！';
  
  drupal_set_title('删除过会企业公示');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('enterprise_announce_deleate_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function enterprise_announce_deleate_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('enterprise_announce_deleate_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
  );
  
  return $form;
}

function enterprise_announce_deleate_form_back($form, &$form_state){
	if(empty($_SESSION['enterprise_announce_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'enterprise_announce/list';
	  }else{
		  $values = $_SESSION['enterprise_announce_edit_form'];
		  unset($_SESSION['enterprise_announce_edit_form']);
		  $form_state['redirect'] = 'enterprise_announce/edit/'.$values['bid'];
	  }
}

function enterprise_announce_deleate_form_submit($form, &$form_state){
  //简单的重定向到contactus页面
 if(empty($_SESSION['enterprise_announce_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'enterprise_announce/list';
	  }else{
		  $values = $_SESSION['enterprise_announce_edit_form'];
		  unset($_SESSION['enterprise_announce_edit_form']);
		  db_delete('enterprise_announce')
			->condition('id' ,$values['bid'])
			->execute();
		  $form_state['redirect'] = 'enterprise_announce/list';;
	  }

}

function enterprise_announce_publish_form(){
	 //$form['bid'] = array('#type' => 'hidden', '#value' => 0);
		 
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['title'] = array(
	  '#title' => t('标题'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['title'])?$_SESSION['enterprise_announce_publish_form']['title']:"",
	 );
	 /*
	 $form['abstract'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['abstract'])?$_SESSION['enterprise_announce_publish_form']['abstract']:"",
	  );
	 
	 $form['keywords'] = array(
	  '#title' => t('关键字'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['keywords'])?$_SESSION['enterprise_announce_publish_form']['keywords']:"",
	 );
	 */
	 $today = getdate();
	 $todaytimestamp =  mktime(0, 0, 0,$today['mon'], $today['mday'], $today['year']);
	  
 	 $enddatetime =  getdate($todaytimestamp+7*24*3600);
 	 $form['timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('开始时间'),
	     '#default_value' => array('year' =>  $today['year'], 'month' =>  $today['mon'], 'day' => $today['mday']), 
	  );
     
     $form['endtimestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('结束时间'),
	     '#default_value' => array('year' =>  $enddatetime['year'], 'month' =>  $enddatetime['mon'], 'day' => $enddatetime['mday']), 
	  );
	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  $fund_list = get_part_by_state(9);
	  $header = array(
	    'nishejijinmingcheng' => t('拟申请基金名称'),
	    'jijinguimo' => t('基金规模'),
	    'zhuyaotouzilingyu' => t('主投领域'),
	    'guanlijigoumingcheng' => t('机构名称'),
	    'lianxiren' => t('联系人'),
	  );  
	
		 
	  $options = array();
	  foreach ($fund_list as $fund) {
	    $options[$fund['id']] =array(
	      'nishejijinmingcheng' => $fund['nishejijinmingcheng'],
	      'jijinguimo' => $fund['jijinguimo'],  
	      'zhuyaotouzilingyu' => $fund['zhuyaotouzilingyu'],
	      'guanlijigoumingcheng' => $fund['guanlijigoumingcheng'],
	      'lianxiren' => $fund['lianxiren'],  
	    );
	  }
	
	  $form['table'] = array(
	    '#type' => 'tableselect',
	    '#header' => $header,
	    
	    '#options' => $options,
	    '#empty' => t('没有基金记录'),
	    
	  );
	  
	 $form['body'] = array(
	  '#title' => t('正文'),
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['body'])?$_SESSION['enterprise_announce_publish_form']['body']:"",
	 );
	 
	 
  
	 $form['back'] = array(
	    '#type' => 'submit',
	    '#value' => t('清空'),
	    '#submit' => array('enterprise_announce_publish_form_back'),
	  );
	  
	 $form['save'] = array(
	    '#type' => 'submit',
	    '#value' => t('暂存'),
	    '#submit' => array('enterprise_announce_publish_form_save'),
	  );
	  
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('发布（提交后公示企业列表不可修改）'),
	  );
	  return $form;
}
/*
function enterprise_announce_publish_perform($form, &$form_state, $perid){	 
	$form['bid'] = array('#type' => 'hidden', '#value' => $perid);
	
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['title'] = array(
	  '#title' => t('标题'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['title'])?$_SESSION['enterprise_announce_publish_form']['title']:"",
	 );
	 $form['body'] = array(
	  '#title' => t('正文'),
	  '#type' => 'text_format',
      '#base_type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['enterprise_announce_publish_form']['body'])?$_SESSION['enterprise_announce_publish_form']['body']:"",
	 );
	 	 
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('确认'),
	  );
	  return $form;
}


function enterprise_announce_publish_perform_validate($form, &$form_state){
	  if (drupal_strlen($form_state['values']['title']) > 255) {
	    form_set_error('title', '标题长度不能大于 255 个字符');
	  } else if (drupal_strlen($form_state['values']['keywords']) > 255) {
	    form_set_error('keywords', '关键字长度不能大于 255 个字符');
	  }
  
}

function enterprise_announce_publish_perform_submit($form, &$form_state){
	 $_SESSION['enterprise_announce_publish_form'] = $form_state['values'];
	 $form_state['redirect'] = 'enterprise_announce/confirm';
}
*/
function enterprise_announce_publish_form_validate($form, &$form_state){
	  if (drupal_strlen($form_state['values']['title']) > 255) {
	    form_set_error('title', '标题长度不能大于 255 个字符');
	  }  
}

function enterprise_announce_publish_form_back($form, &$form_state){
 $form_state['redirect'] = 'enterprise_announce/publish';
}

function enterprise_announce_publish_form_submit($form, &$form_state){
		/**
	   * 写入数据库
	   */
	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
	  $values = $form_state['values'];
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($values)){
		  drupal_goto('enterprise_announce/publish');
		  //$form_state['redirect'] = 'enterprise_announce/publish';
	  }
	  $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $datetime =  mktime(0, 0, 0,$values['timestamp']['month'], $values['timestamp']['day'], $values['timestamp']['year']);
	  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);

	
	 $funds_list = array();
	 foreach ($values['table'] as $rows) {
         $funds_list[] =$rows;
     }

	 $funds_announce_list = '';
	 $pici_id = get_pici_id_bystate(5);
	 $funds_str_list ='';
	 $funds_array = get_part_by_state(9);
	 foreach ($funds_array as $fund) {
	 	 $funds_announce_list .= $fund['id'].',';
         if(in_array($fund['id'],$funds_list, TRUE)&&$funds_list!=NULL){
         	set_state_by_id(11,$fund['id']);
			$$funds_str_list .= $fund['id'].',';
         }
		 else{
		 	set_state_by_id(10,$fund['id']);
		 }
     }
	 
     set_finalannounce_pici_fundslist($funds_announce_list);
	 set_pici_state(5);
	  $last_id = db_insert('enterprise_announce')
  		->fields(array(
  			 'pici_id' => $pici_id,
   			 'title' => $values['title'],
   			 'endtimestamp' =>  $enddatetime,
   			 'timestamp' => $datetime,
   			 'body' => $page_content,
   			 'funds_list' => $$funds_str_list,
   			 'state' => 1,
  			))->execute();
	  if ($last_id!=NULL) {
			 $form_state['redirect'] = 'enterprise_announce/list';
		 	    
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }
		
	
}

function enterprise_announce_publish_form_save($form, &$form_state){
	  
	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
	  $values = $form_state['values'];
	  if(empty($values)){
		  drupal_goto('enterprise_announce/publish');
		  //$form_state['redirect'] = 'enterprise_announce/publish';
	  }
	  $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $datetime =  mktime(0, 0, 0,$values['timestamp']['month'], $values['timestamp']['day'], $values['timestamp']['year']);
	  $enddatetime = mktime(0, 0, 0,$values['endtimestamp']['month'], $values['endtimestamp']['day'], $values['endtimestamp']['year']);
	  $funds_list = array();
	  foreach ($values['table'] as $rows) {
         $funds_list[] =$rows;
      }
      $funds_str_list = '';
	  $funds_announce_list = '';
	  
	  
	  $funds_array = get_part_by_state(9);
	  $pici_id = get_pici_id_bystate(5);
	  
	  foreach ($funds_array as $fund) {
	  	 $funds_announce_list .= $fund['id'].',';
         if(in_array($fund['id'],$funds_list, TRUE)&&$funds_list!=NULL){       	
			$funds_str_list .= $fund['id'].',';
         }
      }
	  set_finalannounce_pici_fundslist($funds_announce_list);
	  set_pici_state(5);
	  $last_id = db_insert('enterprise_announce')
  		->fields(array(
  		     'pici_id' => $pici_id,
   			 'title' => $values['title'],	
   			 'endtimestamp' =>  $enddatetime,
   			 'timestamp' => $datetime,
   			 'body' => $page_content,
   			 'funds_list' => $$funds_str_list,
   			 'state' => 0,
  			))->execute();
	  if ($last_id!=NULL) {
			 $form_state['redirect'] = 'enterprise_announce/list/';
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }	
}

/*
function enterprise_announce_confirm_form_back($form, &$form_state){
 $form_state['redirect'] = 'enterprise_announce/publish';
}
function enterprise_announce_confirm_form(){
	 $body = isset($_SESSION['enterprise_announce_publish_form']['body'])?$_SESSION['enterprise_announce_publish_form']['body']:"";
	 $page_content = '';
	 foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	 }
     $page_content = str_replace(array("\n", "\r\n") , "<BR />", $page_content);
 $form['title'] = array(
  '#title' => t('标题'),
  //表单元素的类型,这里为item
  '#type' => 'item',
  '#markup' => isset($_SESSION['enterprise_announce_publish_form']['title'])?$_SESSION['enterprise_announce_publish_form']['title']:"",
 );
 
  //表单元素“单位名称”
 $form['abstract'] = array(
  '#title' => t('关键字'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['enterprise_announce_publish_form']['abstract'])?$_SESSION['enterprise_announce_publish_form']['abstract']:"",
 );
 
 //表单元素“电子邮件”
 $form['keywords'] = array(
  '#title' => t('关键字'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['enterprise_announce_publish_form']['keywords'])?$_SESSION['enterprise_announce_publish_form']['keywords']:"",
 );
 
 //表单元素“电话号码”
 $form['body'] = array(
  '#title' => t('正文'),
  '#type' => 'item',
  '#markup' => isset($page_content)?$page_content:"",
 );
 
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('返回'),
    '#submit' => array('enterprise_announce_confirm_form_back'),
  );

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('暂存'),
    '#submit' => array('enterprise_announce_confirm_form_save'),
  );
   
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('提交'),
  );
  
  return $form;
}
function enterprise_announce_confirm_form_submit($form, &$form_state){
	   ////
	   // 写入数据库
	   ////
	   $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['enterprise_announce_publish_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'enterprise_announce/publish';
	  }else{
		  $values = $_SESSION['enterprise_announce_publish_form'];
		  unset($_SESSION['enterprise_announce_publish_form']);
	  }
	  $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $last_id = db_insert('enterprise_announce')
  		->fields(array(
   			 'title' => $values['title'],
   			 'abstract' => $values['abstract'],
   			 'keywords' =>  $values['keywords'],
   			 'timestamp' => $_SERVER['REQUEST_TIME'],
   			 'body' => $page_content,
   			 'state' => 1,
  			))->execute();
	  if ($last_id!=NULL) {
	    
	    if($values['bid']>0)
	  		{
	  			db_update('guiding_fund_info')
		  		->fields(array(
		   			 'enterprise_announce_id' => $last_id,
		  			))
		  		->condition('id', $values['bid'])
		  		->execute();
				$form_state['redirect'] = 'guidingfund/list';
	  		}
		else
			 $form_state['redirect'] = 'enterprise_announce/list/'.$last_id;
		 	    
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }
		
	
}

function enterprise_announce_confirm_form_save($form, &$form_state){
	   ////
	   // 写入数据库
	   ////
  
	   $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['enterprise_announce_publish_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'enterprise_announce/publish';
	  }else{
		  $values = $_SESSION['enterprise_announce_publish_form'];
		  unset($_SESSION['enterprise_announce_publish_form']);
	  }
	  $body = $values['body'];
	  $page_content = '';
	  foreach($body as $content){
	 	if($content != 'plain_text')
	 	 	$page_content .= str_replace(array("\n", "\r\n") , "<BR />", $content);
	  }
	  $last_id = db_insert('enterprise_announce')
  		->fields(array(
   			 'title' => $values['title'],
   			 'abstract' => $values['abstract'],
   			 'keywords' =>  $values['keywords'],
   			 'timestamp' => $_SERVER['REQUEST_TIME'],
   			 'body' => $page_content,
   			 'state' => 0,
  			))->execute();
	  if ($last_id!=NULL) {
	    
	    if($values['bid']>0)
	  		{
	  			db_update('guiding_fund_info')
		  		->fields(array(
		   			 'enterprise_announce_id' => $last_id,
		  			))
		  		->condition('id', $values['bid'])
		  		->execute();
				$form_state['redirect'] = 'guidingfund/list';
	  		}
		else
			 $form_state['redirect'] = 'enterprise_announce/list/'.$last_id;
		 	    
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }
		
	
}
*/